/**
 * Copyright (C) 2012 The SkyTvOS Project
 * <p/>
 * Version     Date           Author
 * ─────────────────────────────────────
 * 2014年12月27日         Root.Lu
 */

package com.coocaa.launcher.statusplugins.weather;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.coocaa.launcher.framework.launcherhost.statusbar.StatusBarPluginView;
import com.coocaa.launcher.statusplugins.weather.StatusPluginWeather.IStatusPluginWeather;
import com.coocaa.x.framework.utils.LogUtils;
import com.skyworth.webSDK.webservice.weather.domian.DayWeather;
import com.tianci.appstore.R;
import com.tianci.appstore.utils.SkyConstants;

import java.util.List;

public class StatusPluginWeatherView extends StatusBarPluginView implements IStatusPluginWeather {
    private TextView textCity = null;
    Context context;
    LinearLayout main, layout;
    private final int DAYCOUNT = 3;
    private Animation animLoading, animHiddening;
    private boolean isAlive = true;
    int what = 1;

    public StatusPluginWeatherView(Context mcontext, String name) {
        super(mcontext, name);
        // TODO Auto-generated constructor stub
        context = mcontext;

        layout = new LinearLayout(context);
        textCity = new TextView(context);
        textCity.setTextAppearance(context, R.style.statusbar_plugin_text_style);
        textCity.setTextSize(SkyConstants.Dpi(32));
        LinearLayout.LayoutParams cityparams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        cityparams.topMargin = SkyConstants.Div(3);
        layout.addView(textCity, cityparams);

        main = new LinearLayout(context);
        main.setOrientation(LinearLayout.VERTICAL);
        initView();
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, SkyConstants.Div(50));
        params.leftMargin = SkyConstants.Div(20);
        layout.addView(main, params);
        addView(layout);
    }

    private void initView() {

        for (int i = 0; i < DAYCOUNT; i++) {
            WeatherView weatherView = new WeatherView(context);
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            main.addView(weatherView, params);
        }
        initAnimation();
        mHandler.postDelayed(runnable, 5000);
    }

    private Runnable runnable = new Runnable() {
        @Override
        public void run() {
            // TODO Auto-generated method stub
            Message message = new Message();
            message.what = what;
            mHandler.sendMessage(message);
            LogUtils.d(" getCity mHandler:" + what);
            if (what >= 2) {
                what = 0;
            } else {
                what++;
            }

            mHandler.postDelayed(this, 5000);
        }
    };

    @Override
    public int getConfigGravity() {
        // TODO Auto-generated method stub
        return Gravity.CENTER;
    }

    @Override
    public void update(final String city, final List<DayWeather> dayWeathers) {
        if (dayWeathers == null || dayWeathers.size() == 0)
            return;
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                // TODO Auto-generated method stub
                textCity.setText(city);
                for (int i = DAYCOUNT - 1; i >= 0; i--) {
                    if(dayWeathers.size()>i){
                        final DayWeather dayWeather = dayWeathers.get(i);
                        ((WeatherView) main.getChildAt(i)).setWeather(city, dayWeather, i);
                    }
                }
            }
        });
        mHandler.removeCallbacksAndMessages(null);
        mHandler.postDelayed(runnable, 5000);
    }

    @Override
    public void changeView(final boolean isVisible) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                if(isVisible){
                    layout.setVisibility(View.VISIBLE);
                }else{
                    layout.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    private void initAnimation() {
        animLoading = new TranslateAnimation(0, 0, -100, 0);
        animLoading.setDuration(1000);
        animHiddening = new TranslateAnimation(0, 0, 0, 100);
        animHiddening.setDuration(1000);
    }

    private void startLoadingAnim(View view) {
        view.clearAnimation();
        view.startAnimation(animLoading);
        view.setVisibility(View.VISIBLE);
    }

    private void startHiddeningAnim(View view) {
        view.clearAnimation();
        view.startAnimation(animHiddening);
        view.setVisibility(View.GONE);
    }

    private void hideView(View view) {
        view.clearAnimation();
        view.setVisibility(View.GONE);
    }

    Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            final int flag = msg.what % DAYCOUNT;
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    if (flag == 0) {
                        startLoadingAnim(main.getChildAt(0));
                        startHiddeningAnim(main.getChildAt(2));
                        hideView(main.getChildAt(1));
                    } else if (flag == 1) {
                        startLoadingAnim(main.getChildAt(1));
                        startHiddeningAnim(main.getChildAt(0));
                        hideView(main.getChildAt(2));
                    } else if (flag == 2) {
                        startLoadingAnim(main.getChildAt(2));
                        startHiddeningAnim(main.getChildAt(1));
                        hideView(main.getChildAt(0));
                    }
                }
            });

        }
    };

    public void destroy() {
        mHandler.removeCallbacksAndMessages(null);
    }
}
